package de.fhb.equakecamel;

import org.apache.camel.Exchange;
import org.apache.camel.language.XPath;

public class Normalizer {

	public void gfzpToEarthquake(Exchange _exchange, @XPath("/rss/channel/item/title") String _title, @XPath("/rss/channel/item/description") String _desc){
		_title = _title.replace("<title>", "").replace("</title>", "");
		_desc = _desc.replace("<description>", "").replace("</description>", "");
		
		String[] oha = _desc.substring(22).replaceAll("   ", " ").split(" ");
		String latitude = "";
		String longitude = "";
		String area = "";
		if(oha[1].equals("N")){
			latitude = oha[0];
        	if(oha[3].equals("E")){
        		area = "NE";
        	}else{
        		area = "NW";
        	}
        }else{
        	latitude = "-" + oha[0];
        	if(oha[3].equals("E")){
        		area = "SE";
        	}else{
        		area = "SW";
        	}
        }
        if(oha[3].equals("E")){	
        	longitude = oha[2];
        }else{
        	longitude = "-" + oha[2];
        }
//		System.out.println("---");
//		System.out.println(oha[1] +" "+ oha[0] +"  "+  oha[3] +" "+ oha[2]);
//		System.out.println(latitude+"  "+longitude);
//        System.out.println(area.substring(0,1) + "         " + area.substring(1));
        
        _exchange.getOut().setBody(createEarthquake(_title.substring(7), _desc.substring(0, 19), _title.substring(2,5), latitude, longitude, oha[4], area));
	}
	
	public void usgsToEarthquake(Exchange _exchange, @XPath("rss/channel/item") String _item){
		
		String[] oha = _item.split("      ");
		
		String title = oha[1].replace("<title>", "").replace("</title>\r\n", "");
		String latitude = oha[3].replace("<geo:lat xmlns:geo=\"http://www.w3.org/2003/01/geo/wgs84_pos#\">", "").replace("</geo:lat>\r\n", "");
		String longitude = oha[4].replace("<geo:long xmlns:geo=\"http://www.w3.org/2003/01/geo/wgs84_pos#\">", "").replace("</geo:long>\r\n", "");
		String depth = oha[10].replace("<dc:subject xmlns:dc=\"http://purl.org/dc/elements/1.1/\">", "").replace(" km</dc:subject>\r\n", "");
		String date = oha[11].replace("<dc:date xmlns:dc=\"http://purl.org/dc/elements/1.1/\">", "").replace("Z</dc:date>\r\n    </item>", "").replace("T", " ");
		String area = "";
		if(Double.parseDouble(latitude) > 0){
        	if(Double.parseDouble(longitude) > 0){
        		area = "NE";
        	}else{
        		area = "NW";
        	}
        }else{
        	if(Double.parseDouble(longitude) > 0){
        		area = "SE";
        	}else{
        		area = "SW";
        	}
        }
		
//		System.out.println("----");
//		System.out.println(latitude +"  "+ longitude);
//		System.out.println(Double.parseDouble(latitude) +"  "+ Double.parseDouble(longitude));
//		System.out.println(area.substring(0,1) + "         " + area.substring(1));
		
		_exchange.getOut().setBody(createEarthquake(title.substring(7), date, title.substring(2,5), latitude, longitude, depth, area));
	}
	
	private String createEarthquake(String _region, String _date, String _mag, String _lat, String _long, String _depth, String _area){
		String eq = "";
		
//		eq += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 
		eq += "<earthquake>\n";
        eq += "  <region>" + _region + "</region>\n";
        eq += "  <date>" + _date + "</date>\n";
        eq += "  <magnitude>" + _mag + "</magnitude>\n";
        eq += "  <latitude>" + _lat + "</latitude>\n";
        eq += "  <longitude>" + _long + "</longitude>\n";
        eq += "  <depth>" + _depth + "</depth>\n";
        eq += "  <area>" + _area +"</area>\n";
        eq += "</earthquake>";
        
		return eq;
	}
}
